<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/13
 * Time: 下午2:44
 */

namespace app\src\userRoles\logic;


use app\src\base\logic\BaseLogic;
use app\src\user\enum\RoleEnum;
use app\src\userRoles\model\UserRoles;

/**
 * 用户前台角色
 * Class UserRolesLogic
 * @package app\src\userRoles\logic
 */
class UserRolesLogic extends BaseLogic
{
    protected function _init()
    {
        $this->setModel(new UserRoles());
    }

    /**
     * 判断是否拥有某个角色
     * @param $uid
     * @param $role_code
     * @return bool
     */
    public function hasRole($uid,$role_code)
    {

        $role_id = $this->getRoleID($role_code);

        if($role_id > 0){

            $result = $this->getInfo(['uid'=>$uid,'group_id'=>$role_id]);
            if(!$result['status'] || empty($result['info'])) return false;
            else return true;
        }

        return false;
    }

    public function getRoleID($role_code){
        $role_id = -1;
        switch ($role_code) {
            case RoleEnum::ROLE_User:
                $role_id = RoleEnum::getRoleIDBy($role_code);
                break;
            case RoleEnum::ROLE_Seller:
                $role_id = RoleEnum::getRoleIDBy($role_code);
                break;
            case RoleEnum::ROLE_Waiter:
                $role_id = RoleEnum::getRoleIDBy($role_code);
                break;
            default:
                break;
        }
        return $role_id;
    }

    /**
     * 添加用户角色
     * @param $uid
     * @param $role_code
     */
    public function addRole($uid, $role_code, $account_id = 0)
    {
        $result = $this->hasRole($uid, $role_code);
        if($result){
            return true;
        }
        $role_id = $this->getRoleID($role_code);
        $entity = [
            'uid' => $uid,
            'group_id' => $role_id,
            'account_id' => $account_id
        ];
        $result = $this->saveAll([$entity]);
        return $result['status'];
    }

    /**
     * 删除用户角色
     * @param $uid
     * @param $role_code
     */
    public function removeRole($uid, $role_code, $account_id = false)
    {
        $role_id = $this->getRoleID($role_code);
        $map = [
            'uid' => $uid,
            'group_id' => $role_id
        ];

        if($account_id!==false){
            $map['account_id'] = $account_id;
        }
        $result = $this->delete($map);
        return $result['status'];
    }

    /**
     * 获取用户所有角色
     * @param $uid
     */
    public function getRoles($uid)
    {
        $map = ['uid' => $uid];
        $result = $this->queryNoPaging($map);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        $roles = [];
        foreach ($result['info'] as $role){
            $group_name = '';
            switch ($role['group_id']) {
                case 1:
                    $group_name = RoleEnum::ROLE_User;
                    break;
                case 2:
                    $group_name = RoleEnum::ROLE_Seller;
                    break;
                case 3:
                    $group_name = RoleEnum::ROLE_Waiter;
                    break;
                default:
                    break;
            }
            $roles[] = [
                'group_name' => $group_name,
                'group_id' => $role['group_id'],
                'account_id' => $role['account_id']
            ];
        }
        return $this->apiReturnSuc($roles);
    }

}